package com.jams.goodox.activity.mine.interaction;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hangzhou.jin.customview.JRecyclerView;
import com.hangzhou.jin.customview.RecycleViewDivider;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.activity.serve.PersonalRentingDetailActivity;
import com.jams.goodox.activity.serve.enterprise.EnterpriseCooperationDetailActivity;
import com.jams.goodox.entity.CommentBean;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DataCacheManager;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.ToastUtil;
import com.jams.library.fresco.FrescoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;


/**
 * Created by jams_zhen on 2017/5/9.
 * 我的评论
 */

public class MineCommentFragment extends BaseFragment {
    @Bind(R.id.jRecyclerView)
    JRecyclerView jRecyclerView;

    BaseQuickAdapter mAdapter;
    int curPage = 1;
    private List<CommentBean.DataBean.ResultListBean> resultList;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.j_recyclerview, container, false);
        ButterKnife.bind(this, view);
      
        jRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new BaseQuickAdapter<CommentBean.DataBean.ResultListBean>(R.layout.item_mine_comment, new ArrayList<CommentBean.DataBean.ResultListBean>()) {
         
            @Override
            protected void convert(BaseViewHolder baseViewHolder, final CommentBean.DataBean.ResultListBean o) {
                SimpleDraweeView imageView = baseViewHolder.getView(R.id.img_icon);
                FrescoUtil.loadNetUrl(imageView, TextUtils.isEmpty(o.getFromUser().getHeadPicName()) ? "www" : o.getFromUser().getHeadPicName());
                StringBuilder sb = new StringBuilder();
                sb
                   .append(o.getFromUser().getNickname())
                   .append("@")
                   .append(DataCacheManager.getInstance().getUser().getNickName())
                   .append(" : ")
                   .append(o.getMsgContent());
                baseViewHolder.setText(R.id.tx_comment_reply_people, sb)
                        .setText(R.id.tx_time, o.getIntime());
            }
        };
        
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                final CommentBean.DataBean.ResultListBean  o = resultList.get(i);
                Intent intent;
                switch (StringUtils.newString(o.getType())){
                    case "0"://租房
                        intent = new Intent(mContext, PersonalRentingDetailActivity.class);
                        intent.putExtra(PersonalRentingDetailActivity.ID, StringUtils.newString(o.getNeighborId()));
                        startActivity(intent);
                        break;
                    case "1"://***活动
                        break;
                    case "3"://企业合作
                        intent = new Intent(mContext, EnterpriseCooperationDetailActivity.class);
                        intent.putExtra(EnterpriseCooperationDetailActivity.COOPERATION, StringUtils.newString(o.getNeighborId()));
                        startActivity(intent);
                    default:
                        break;
                }
                
                
            }
        });
        jRecyclerView.setAdapter(mAdapter);
        // 设置8dp的间隙
       /* int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.margin_size_small);
        jRecyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));*/
        jRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.base_background)));
        //jRecyclerView.MarginTop(R.dimen.margin_size_small);
        mAdapter.openLoadMore(PAGE_SIZE, true);
        jRecyclerView.setBackgroundResource(R.color.gray_background);
        // 自定义上拉View
        mAdapter.setLoadingView(getLoadMoreBootView(mContext));

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadData(false);
            }
        });

        loadData(true);

        jRecyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 刷新代码
                loadData(true);
            }
        });

        return view;
    }

    private void loadData(final boolean isFirstPage) {
        if (isFirstPage) curPage = 1;
        else curPage++;
        enqueue(createCallService(MineService.class).myNoticeInteraction(curPage, PAGE_SIZE), new RetrofitCallbackListener<CommentBean>() {
            @Override
            public void onSuccess(Call<CommentBean> call, CommentBean result) {
                jRecyclerView.setRefreshing(false);
                jRecyclerView.loadComplete();
                if (null != result.getData()) {
                    resultList = result.getData().getResultList();
                    if (null != resultList) {
                        if (isFirstPage) {
                            mAdapter.setNewData(resultList);
                            mAdapter.notifyDataChangedAfterLoadMore(resultList.size() == PAGE_SIZE);
                            if (resultList.size() == 0) {
                                jRecyclerView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
                                // return;
                            }
                        } else {
                            mAdapter.notifyDataChangedAfterLoadMore(resultList, resultList.size() == PAGE_SIZE);
                        }
                    }
                }
            }

            @Override
            public void onError(String msg) {
                ToastUtil.show(msg);
                if (!CollectionUtils.hasData(resultList)) {
                    jRecyclerView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
                } else {
                    jRecyclerView.loadComplete();
                }
                jRecyclerView.setRefreshing(false);

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
